package com.framework.core.utils;

import java.util.Map;

import com.framework.core.myexception.NoSuchSqlException;

//dao代理method匹配sql
public class SqlMappingUtils {

	private static SqlMappingUtils sqlMappingUtils;

	private static Map<String, Map<String, Object>> map;

	private SqlMappingUtils() {

	}

	public synchronized static SqlMappingUtils getInstance() {

		if (null == sqlMappingUtils) {
			sqlMappingUtils = new SqlMappingUtils();
		}
		return sqlMappingUtils;

	}

	// 根据nameSpace和id获取sql
	public static Object getSqlById(String nameSpace, String id) throws NoSuchSqlException {
		if (map.containsKey(nameSpace)) {
			Map<String, Object> m = map.get(nameSpace);
			if (m.containsKey(id)) {
				return m.get(id);
			} else {
				System.err.println("找不到ID为：" + id + "sql");
				throw new NoSuchSqlException("找不到ID为：" + id + "sql");
			}
		} else {
			System.err.println("找不到nameSpace为：" + nameSpace + "的xml配置文件");
			throw new NoSuchSqlException("找不到nameSpace为：" + nameSpace + "的xml配置文件");
		}
	}

	public static Map<String, Map<String, Object>> getMap() {
		return map;
	}

	public static void setMap(Map<String, Map<String, Object>> map) {
		SqlMappingUtils.map = map;
	}

}
